using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Net;
using System.Xml;

namespace Xyne.Actions
{
    public enum RepeatHealthEffects
    {
        None,
        Burn,
        Blizzard,
        HealingRain,
        Flood
    }

    public class RepeatHealthAction : Action
    {
        private float healthChange;
        public float HealthChange
        {
            get { return healthChange; }
            set { healthChange = value; }
        }

        private int numTicks;
        public int BaseNumTicks
        {
            get { return numTicks; }
            set { numTicks = value; }
        }

        public int NumTicks
        {
            get { return (int)(numTicks * Level); }
        }

        private float secondsPerTick;
        public float SecondsPerTick
        {
            get { return secondsPerTick; }
            set { secondsPerTick = value; }
        }

        private RepeatHealthEffects effect = RepeatHealthEffects.None;
        public RepeatHealthEffects Effect
        {
            get { return effect; }
            set { effect = value; }
        }

        public RepeatHealthAction()
        {
            Type = ActionTypes.RepeatHealth;
        }

        public RepeatHealthAction(Action action)
            : base(action)
        {
            Type = ActionTypes.RepeatHealth;

            if (action is RepeatHealthAction)
            {
                RepeatHealthAction repeat = action as RepeatHealthAction;
                HealthChange = repeat.HealthChange;
                BaseNumTicks = repeat.BaseNumTicks;
                SecondsPerTick = repeat.SecondsPerTick;
                Effect = repeat.Effect;
            }
        }

        public override void Serialize(PacketWriter writer)
        {
            base.SerializeAction(writer);

            writer.Write(HealthChange);
            writer.Write(BaseNumTicks);
            writer.Write(SecondsPerTick);
            writer.Write((ulong)Effect);
        }

        public static RepeatHealthAction Deserialize(PacketReader reader)
        {
            RepeatHealthAction action = new RepeatHealthAction();

            action.DeserializeAction(reader);
            action.HealthChange = reader.ReadSingle();
            action.BaseNumTicks = reader.ReadInt32();
            action.SecondsPerTick = reader.ReadSingle();
            action.Effect = (RepeatHealthEffects)reader.ReadUInt64();

            return action;
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            writer.WriteElementString("Effect", effect.ToString());
            writer.WriteElementString("HealthChange", healthChange.ToString());
            writer.WriteElementString("NumTicks", numTicks.ToString());
            writer.WriteElementString("SecondsPerTick", secondsPerTick.ToString());
        }

        public override void ReadNode(XmlReader reader)
        {
            if (reader.LocalName == "Effect")
            {
                effect = (RepeatHealthEffects)Enum.Parse(typeof(RepeatHealthEffects), reader.ReadString(),true);
            }
            else if (reader.LocalName == "HealthChange")
            {
                healthChange = float.Parse(reader.ReadString());
            }
            else if (reader.LocalName == "NumTicks")
            {
                numTicks = int.Parse(reader.ReadString());
            }
            else if (reader.LocalName == "SecondsPerTick")
            {
                secondsPerTick = float.Parse(reader.ReadString());
            }
        }
    }
}
